package com.agileai.bpm.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.mvel2.templates.TemplateRuntime;

import com.agileai.bpm.common.BPMConstants.EmbedVariable;
import com.agileai.bpm.domain.running.ActivityInstance;
import com.agileai.bpm.domain.running.ProcessInstance;
import com.agileai.bpm.domain.running.ProcessVariableInstance;
import com.agileai.bpm.manager.PropertiesResourceManager;
import com.agileai.domain.DataRow;

public class VariableInterpreter {
	public static String replaceSimpleVariable(String template,String code,String value){
		String result = null;
		result = template.replaceAll("\\$\\{"+code+"\\}", value);
		return result;
	}
	public static String replaceSimpleVariable(String template,ProcessVariableInstance procVariableInstance){
		String result = null;
		String tempSql = template;
		String code = procVariableInstance.getCode();
		String value = procVariableInstance.getValue();
		result = tempSql.replaceAll("\\$\\{"+code+"\\}", value);
		return result;
	}
	public static String replaceMapVariable(String template,ProcessVariableInstance procVariableInstance){
		String result = null;
		String tempSql = template;
		String code = procVariableInstance.getCode();
		String xml = procVariableInstance.getValue();
		DataRow row = new DataRow();
		row.initFromXML(xml);
		Iterator<String> iter = row.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next();
			String compositeCode = code+"."+key;
			String value = row.getString(key);
			tempSql = tempSql.replaceAll("\\$\\{"+compositeCode+"\\}", value);
		}
		result = tempSql;
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String replaceVariables(String expession,ActivityInstance activityInstance){
		String result = null;
		ProcessInstance processInstance = activityInstance.getProcessInstance();
		HashMap<String, ProcessVariableInstance> varInstMap = processInstance.getVariableInstances();
		
		HashMap vars = new HashMap();
		Iterator<String> iter = varInstMap.keySet().iterator();
		while (iter.hasNext()){
			String key = iter.next();
			ProcessVariableInstance variableInstance = varInstMap.get(key);
			String value = variableInstance.getValue();
			vars.put(key, value);
		}

		
		vars.put(EmbedVariable.bizRecordId, processInstance.getBizRecordId());
		vars.put(EmbedVariable.processId, processInstance.getProcessId());
		vars.put(EmbedVariable.processInstId, processInstance.getId());
		vars.put(EmbedVariable.activityCode, activityInstance.getCode());
		vars.put(EmbedVariable.submiter, processInstance.getLaucherId());
		
		List<String> userCodeList = activityInstance.getTargetUserCodeList();
		if (userCodeList != null && !userCodeList.isEmpty()){
			String targetUserCodes = StringUtils.join(userCodeList, ",");
			vars.put(BPMConstants.EmbedVariable.targetUserCodes, targetUserCodes);
		}

		List<String> userIdList = activityInstance.getTargetUserIdList();
		if (userIdList != null && !userIdList.isEmpty()){
			String targetUserIds = StringUtils.join(userIdList, ",");
			vars.put(BPMConstants.EmbedVariable.targetUserIds, targetUserIds);
		}
		
		PropertiesResourceManager propertiesResourceManager = PropertiesResourceManager.instance();
		vars.put(PropertiesResourceManager.BPMConfigsKey,propertiesResourceManager.getPropertiesConfig());
		
		result = (String)TemplateRuntime.eval(expession,vars);
		
		return result;
	}
}
